// Package models
// @Title  adminUserModel.go
// @Description  管理员模型
// @Author  小斌 (时间 格式是2021/12/02  10:20)
// @Update  小斌 (时间 格式是2021/12/02  10:20)

package models

import (
	"github.com/astaxie/beego/orm"
	"time"
)

// ImageObj 用户角色 实体类
type ImageObj struct {
	Id                 int    `valid:"id"`
	Title                string `valid:"title"`
	ImageUrl               string `valid:"image_url"`
	IsDelete				int `valid:"is_delete"`
	UpdateTime                time.Time `valid:"update_time" orm:"auto_now"`
	AddTime                time.Time `valid:"add_time" orm:"auto_now_add"`
}

func (this *ImageObj) TableName() string {
	return "image"
}

func init() {
	orm.RegisterModel(new(ImageObj))
}

// GetImageList 获取分页数据
func GetImageList(pageSize, page int) (*[]ImageObj, int, error) {
	var (
		list []ImageObj
		err  error
	)
	qs := orm.NewOrm().QueryTable(new(ImageObj)).OrderBy("-add_time")

	_, err = qs.Filter("is_delete",0).Limit(pageSize).Offset((page - 1) * pageSize).RelatedSel().All(&list)
	count, _ := qs.Filter("is_delete",0).Count()
	if err == orm.ErrNoRows || err == orm.ErrMissPK {
		return nil, 0, err
	}

	return &list, int(count), nil
}

// GetImageInfoById 根据用户名获取记录
func GetImageInfoById(id int) (ImageObj,error) {
	var (
		row ImageObj
		err  error
	)

	err = orm.NewOrm().QueryTable(new(ImageObj)).Filter("id", id).Filter("is_delete", 0).One(&row)
	if err == orm.ErrNoRows || err == orm.ErrMissPK {
		return row,nil
	}

	return row,nil
}

// AddImage 添加管理员
func AddImage(title string,imageUrl string) int64 {
	image := ImageObj{
		Title:    title,
		ImageUrl: imageUrl,
		IsDelete: 0,
	}

	id,err := orm.NewOrm().Insert(&image)
	if err == orm.ErrNoRows || err == orm.ErrMissPK {
		return 0
	}

	return id
}

// DeleteImageById 删除图片
func DeleteImageById(id int) int64 {
	image := ImageObj{
		Id:    id,
		IsDelete: 1,
	}
	num,err := orm.NewOrm().Update(&image,"is_delete")
	if err == orm.ErrNoRows || err == orm.ErrMissPK {
		return 0
	}

	return num
}

// DeleteMoreImageById 批量删除图片
func DeleteMoreImageById(ids []string) int64 {

	orm1 := orm.NewOrm()
	qs:= orm1.QueryTable(new(ImageObj))
	num,err:=qs.Filter("id__in",ids).Update(orm.Params{
		"is_delete": 1,
	})
	if err == orm.ErrNoRows || err == orm.ErrMissPK {
		return 0
	}

	return num
}
